#include "TransitionViewComponentInstance.h"
#include "Props.h"
#include <folly/dynamic.h>

using namespace rnoh;

TransitionViewComponentInstance::TransitionViewComponentInstance(Context context)
    : CppComponentInstance(std::move(context)) {
    m_TransitionNode.setTransitionViewDelegate(this);
}

void TransitionViewComponentInstance::onChildInserted(ComponentInstance::Shared const &childComponentInstance,
                                                      std::size_t index) {
    CppComponentInstance::onChildInserted(childComponentInstance, index);
    m_TransitionNode.insertChild(childComponentInstance->getLocalRootArkUINode(), index);
}

TransitionViewNode &TransitionViewComponentInstance::getLocalRootArkUINode() { return m_TransitionNode; }

void TransitionViewComponentInstance::onChildRemoved(ComponentInstance::Shared const &childComponentInstance) {
    CppComponentInstance::onChildRemoved(childComponentInstance);
    m_TransitionNode.removeChild(childComponentInstance->getLocalRootArkUINode());
}

void TransitionViewComponentInstance::onPropsChanged(SharedConcreteProps const &props) {
    CppComponentInstance::onPropsChanged(props);
    if (!m_props || props->transitionText != m_props->transitionText) {
        m_TransitionNode.setLabel(props->transitionText);
    }
}

void TransitionViewComponentInstance::onTransitionClick() {
    facebook::react::TransitionViewEventEmitter::OnTransitionClick m_onTransitionClick;
    m_onTransitionClick.isTransitionClick = true;
    m_onTransitionClick.type = "custom";
    m_eventEmitter->onTransitionClick(m_onTransitionClick);
    
}

void TransitionViewComponentInstance::handleCommand(std::string const &commandName, folly::dynamic const &args) {
    if (commandName == "changeTransitionText") {
        m_TransitionNode.setLabel(args[0].asString());
    }
}
